package com.roncoo.education.user.service.auth.biz;

import javax.validation.constraints.NotNull;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.roncoo.education.common.config.ThreadContext;
import com.roncoo.education.common.core.base.Result;
import com.roncoo.education.common.core.enums.StatusIdEnum;
import com.roncoo.education.common.core.tools.BeanUtil;
import com.roncoo.education.common.service.BaseBiz;
import com.roncoo.education.user.dao.LecturerDao;
import com.roncoo.education.user.dao.UsersDao;
import com.roncoo.education.user.dao.impl.mapper.entity.Lecturer;
import com.roncoo.education.user.dao.impl.mapper.entity.Users;
import com.roncoo.education.user.service.auth.req.AuthUsersPwdReq;
import com.roncoo.education.user.service.auth.req.AuthUsersReq;
import com.roncoo.education.user.service.auth.resp.AuthLecturerResp;
import com.roncoo.education.user.service.auth.resp.AuthUsersResp;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.DigestUtil;
import lombok.RequiredArgsConstructor;

/**
 * AUTH-用户信息
 *
 * @author wujing
 */
@Component
@RequiredArgsConstructor
public class AuthUsersBiz extends BaseBiz {

    @NotNull
    private final UsersDao dao;
    
    @NotNull
    private final LecturerDao lecturerDao; 

    public Result<AuthUsersResp> view() {
        Users users = dao.getById(ThreadContext.userId());
        if (users != null && users.getStatusId().equals(StatusIdEnum.YES.getCode())) {
        	
        	Lecturer lecturer = lecturerDao.getByLecturerMobile(users.getMobile());
        	
        	AuthLecturerResp lecturerResp = BeanUtil.copyProperties(lecturer, AuthLecturerResp.class);
        	
        	AuthUsersResp authUsersResp = BeanUtil.copyProperties(users, AuthUsersResp.class);
        	authUsersResp.setLecturer(lecturerResp);
        	
            return Result.success(authUsersResp);
        }
        return Result.error("用户不存在或被禁用");
    }

    public Result<String> update(AuthUsersReq req) {
        Users users = BeanUtil.copyProperties(req, Users.class);
        users.setId(ThreadContext.userId());
        int result = dao.updateById(users);
        if (result > 0) {
            return Result.success("操作成功");
        }
        return Result.error("操作失败");
    }
    
    public Result<String> updatePwd(AuthUsersPwdReq req) {
    	Users users = dao.getById(req.getId());
    	
        // 密码校验
        if (StringUtils.isEmpty(req.getPassword1()) || StringUtils.isEmpty(req.getPassword2())) {
            return Result.error("新密码不能为空");
        }        

        if (!req.getPassword1().equals(req.getPassword2())) {
            return Result.error("两次密码不一致");
        }
        
        if (req.getPassword1().length() < 6 || req.getPassword1().length() > 20) {
            return Result.error("密码长度不符合规范（6-20位）");
        }

        if (!users.getMobile().equals(req.getMobile())) {
        	return Result.error("手机号与注册手机号不一致");
        }
        
        users.setMobilePsw(DigestUtil.sha1Hex(users.getMobileSalt() + req.getPassword1()));

        int result = dao.updateById(users);
        if (result > 0) {
            return Result.success("操作成功");
        }
        return Result.error("操作失败");
    }    
}
